SPB Git

Repository Public

page.tsx 5.5 KB 74 lines tsx

1import type { Metadata } from "next";2import { notFound } from "next/navigation";3import { sql } from "@/lib/db";4import { mustParseSheet, readingMinutes, type Sheet } from "@/schema";5import { env } from "@/lib/env";6import { LogoMark } from "@/components/brand";7import { Inline } from "@/renderer/inline";8import { SheetView } from "@/renderer/SheetView";9import { readRenderToken } from "@/server/render-token";1011export const dynamic = "force-dynamic";12export const metadata: Metadata = { robots: { index: false, follow: false } };1314/**15 * Bare render of one stored document, without site chrome, in print mode (interactive blocks show their static16 * state). Opened only by the worker's headless browser with a signed, short-lived token.17 */18export default async function Render({ params }: { params: Promise<{ token: string }> }) {19  const { token } = await params;20  const target = readRenderToken(token);21  if (!target) notFound();22  const [row] = target.kind === "publication"23    ? await sql<{ doc: unknown; slug: string; at: Date }[]>`SELECT p.doc, s.slug, p.published_at AS at FROM sheet_publications p JOIN sheets s ON s.id = p.sheet_id WHERE p.id = ${target.id}`24    : await sql<{ doc: unknown; slug: string; at: Date }[]>`SELECT r.doc, s.slug, r.created_at AS at FROM sheet_revisions r JOIN sheets s ON s.id = r.sheet_id WHERE r.id = ${target.id}`;25  if (!row) notFound();26  const doc = mustParseSheet(row.doc);27  const article = doc.layout.format === "article" && !target.blockId;28  const host = new URL(env.appUrl).host;29  return (30    // A plain block: the sheet is a size container, so it must be given its width by its parent, not shrink-wrapped.31    <div data-render-root style={{ width: "100%" }}>32      {article && <QuickSheetBand doc={doc} url={target.kind === "publication" ? `${host}/s/${row.slug}` : null} date={row.at} />}33      <SheetView doc={doc} locale={doc.metadata.locale} mode="print" assetUrl={(id) => `/render/${token}/asset/${id}`} />34    </div>35  );36}3738const TONE_HEX: Record<string, [string, string]> = { neutral: ["#1f2937", "#475467"], blue: ["#1d3fb8", "#3b6cf6"], teal: ["#0b5d57", "#14a394"], green: ["#14663a", "#22a55b"], amber: ["#8a4b00", "#d98a12"], red: ["#9b1c12", "#e0493a"], violet: ["#5220b8", "#8b5cf6"], pink: ["#a01453", "#e44d93"] };3940/** Title band of the printed quick sheet: what the card is about, for whom, where it comes from. */41function QuickSheetBand({ doc, url, date }: { doc: Sheet; url: string | null; date: Date }) {42  const m = doc.metadata, fr = m.locale === "fr";43  const [a, b] = TONE_HEX[doc.theme.accent] ?? TONE_HEX.blue;44  const h1 = doc.blocks.find((x) => x.type === "heading" && x.level === 1);45  const eyebrow = h1?.type === "heading" && h1.eyebrow ? h1.eyebrow : m.topics.join(" · ");46  const level = { beginner: fr ? "Débutant" : "Beginner", intermediate: fr ? "Intermédiaire" : "Intermediate", advanced: fr ? "Avancé" : "Advanced" }[m.level];47  const chip = { padding: "3px 10px", borderRadius: 999, background: "rgba(255,255,255,0.16)", border: "1px solid rgba(255,255,255,0.28)", font: "600 12px/1.4 var(--font-sans)", whiteSpace: "nowrap" as const };48  return (49    <header data-print-masthead lang={m.locale} style={{ display: "grid", gridTemplateColumns: "minmax(0, 1fr) auto", gap: 28, alignItems: "stretch", padding: "20px 26px", marginBottom: 14, borderRadius: 14, color: "#fff", background: `linear-gradient(120deg, #0e1726 0%, #0e1726 34%, ${a} 78%, ${b} 100%)`, printColorAdjust: "exact", WebkitPrintColorAdjust: "exact" }}>50      <div style={{ minWidth: 0 }}>51        {eyebrow ? <p style={{ margin: 0, font: "650 12px/1.3 var(--font-sans)", letterSpacing: "0.14em", textTransform: "uppercase", color: "rgba(255,255,255,0.72)" }}>{eyebrow}</p> : null}52        <h1 style={{ margin: "6px 0 0", font: "620 40px/1.04 var(--font-display)", letterSpacing: "-0.02em", textWrap: "balance" }}>{m.title}</h1>53        {m.summary ? <p style={{ margin: "10px 0 0", maxWidth: "78ch", font: "400 15.5px/1.42 var(--font-sans)", color: "rgba(255,255,255,0.86)" }}><Inline text={m.summary} /></p> : null}54        <p style={{ display: "flex", flexWrap: "wrap", gap: 8, margin: "12px 0 0" }}>55          <span style={chip}>{level}</span>56          <span style={chip}>{readingMinutes(doc)} min</span>57          {m.tags.slice(0, 4).map((t) => <span key={t} style={chip}>{t}</span>)}58          {m.example ? <span style={{ ...chip, background: "#fdf0d5", color: "#7a4300", border: "1px solid #f3c56b" }}>{fr ? "Exemple — non révisé par l’auteur" : "Example — not reviewed by the author"}</span> : null}59        </p>60      </div>61      <div style={{ display: "flex", flexDirection: "column", justifyContent: "space-between", alignItems: "flex-end", textAlign: "right", gap: 12, font: "500 12.5px/1.45 var(--font-sans)", color: "rgba(255,255,255,0.82)" }}>62        <span style={{ display: "flex", alignItems: "center", gap: 10, color: "#fff" }}>63          <span style={{ display: "inline-flex", borderRadius: 9, boxShadow: "0 0 0 1.5px rgba(255,255,255,0.5)" }}><LogoMark size={34} /></span>64          <span style={{ textAlign: "left" }}><span style={{ display: "block", font: "600 24px/1 var(--font-display)" }}>Sheet</span><span style={{ display: "block", marginTop: 3, font: "500 11.5px/1 var(--font-sans)", color: "rgba(255,255,255,0.8)" }}>by Simon-Pierre Boucher</span></span>65        </span>66        <span>67          {url ?? (fr ? "Brouillon — non publié" : "Draft — not published")}<br />68          {new Intl.DateTimeFormat(fr ? "fr-CA" : "en-CA", { dateStyle: "long", timeZone: "America/Toronto" }).format(date)}69        </span>70      </div>71    </header>72  );73}74
Files main
Menu

Theme